#include "mtTeXRasterizer.h"
#include "nsStringAPI.h"
#include "plbase64.h"
#include "nspr.h"
#include "nsXPCOM.h"
#include <windows.h>

 typedef int (__cdecl *cfunc)(const char * expression, const char *gifFileName);
 cfunc EngineCreate;



NS_IMPL_ISUPPORTS1(mtTeXRasterizer, mtITeXRasterizer)

mtTeXRasterizer::mtTeXRasterizer()
{
  /* member initializers and constructor code */
}

mtTeXRasterizer::~mtTeXRasterizer()
{
  /* destructor code */
}

/* Main function */
NS_IMETHODIMP mtTeXRasterizer::LoaderCreate(const nsACString & aExpression,  const nsACString & aFilename,  const nsACString & aComponentpath, nsAString & _retval)
{

	//loads the dll & runs the exported function ..."as usual"
	//

	size_t len = aComponentpath.Length() + 1;
	WCHAR *swFoo = new WCHAR[len];
	MultiByteToWideChar(CP_ACP, 0, aComponentpath.BeginReading(), len - 1, swFoo, len);
	swFoo[len - 1] = 0;  // Null-terminate it.

	HINSTANCE hLib=LoadLibrary((LPCWSTR) swFoo);
	if(hLib==NULL)
		return NS_ERROR_FAILURE;
	
	EngineCreate=(cfunc)GetProcAddress((HMODULE)hLib, "CreateGifFromEq");
	if(EngineCreate==NULL)
		return NS_ERROR_FAILURE;


	EngineCreate(aExpression.BeginReading(), aFilename.BeginReading());


	FreeLibrary((HMODULE)hLib);
	delete [] swFoo;


  return NS_OK;
}
